<template>
  <el-dialog
    :title="!dataForm.docId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="医师名称" prop="docName">
      <el-input v-model="dataForm.docName" placeholder="医师名称"></el-input>
    </el-form-item>
    <el-form-item label="医师级别" prop="docType">
      <el-input v-model="dataForm.docType" placeholder="医师级别"></el-input>
    </el-form-item>
    <el-form-item label="一级医院id" prop="hosId">
      <el-input v-model="dataForm.hosId" placeholder="一级医院id"></el-input>
    </el-form-item>
    <el-form-item label="二级科室id" prop="departId">
      <el-input v-model="dataForm.departId" placeholder="二级科室id"></el-input>
    </el-form-item>
    <el-form-item label="三级医师级别id" prop="typeId">
      <el-input v-model="dataForm.typeId" placeholder="三级医师级别id"></el-input>
    </el-form-item>
    <el-form-item label="年限" prop="docYear">
      <el-input v-model="dataForm.docYear" placeholder="年限"></el-input>
    </el-form-item>
    <el-form-item label="医师资格证书" prop="docStatus">
      <el-input v-model="dataForm.docStatus" placeholder="医师资格证书"></el-input>
    </el-form-item>
    <el-form-item label="医师执业证书" prop="docPractice">
      <el-input v-model="dataForm.docPractice" placeholder="医师执业证书"></el-input>
    </el-form-item>
    <el-form-item label="问诊次数" prop="askCount">
      <el-input v-model="dataForm.askCount" placeholder="问诊次数"></el-input>
    </el-form-item>
    <el-form-item label="总收入金额" prop="docMoney">
      <el-input v-model="dataForm.docMoney" placeholder="总收入金额"></el-input>
    </el-form-item>
    <el-form-item label="好评度" prop="docGood">
      <el-input v-model="dataForm.docGood" placeholder="好评度"></el-input>
    </el-form-item>
    <el-form-item label="预约人次（不超过5人）" prop="docMake">
      <el-input v-model="dataForm.docMake" placeholder="预约人次（不超过5人）"></el-input>
    </el-form-item>
    <el-form-item label="医生头像" prop="docImg">
      <el-input v-model="dataForm.docImg" placeholder="医生头像"></el-input>
    </el-form-item>
    <el-form-item label="关注人数" prop="docCollect">
      <el-input v-model="dataForm.docCollect" placeholder="关注人数"></el-input>
    </el-form-item>
    <el-form-item label="主治疾病" prop="docInd">
      <el-input v-model="dataForm.docInd" placeholder="主治疾病"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          docId: 0,
          docName: '',
          docType: '',
          hosId: '',
          departId: '',
          typeId: '',
          docYear: '',
          docStatus: '',
          docPractice: '',
          askCount: '',
          docMoney: '',
          docGood: '',
          docMake: '',
          docImg: '',
          docCollect: '',
          docInd: ''
        },
        dataRule: {
          docName: [
            { required: true, message: '医师名称不能为空', trigger: 'blur' }
          ],
          docType: [
            { required: true, message: '医师级别不能为空', trigger: 'blur' }
          ],
          hosId: [
            { required: true, message: '一级医院id不能为空', trigger: 'blur' }
          ],
          departId: [
            { required: true, message: '二级科室id不能为空', trigger: 'blur' }
          ],
          typeId: [
            { required: true, message: '三级医师级别id不能为空', trigger: 'blur' }
          ],
          docYear: [
            { required: true, message: '年限不能为空', trigger: 'blur' }
          ],
          docStatus: [
            { required: true, message: '医师资格证书不能为空', trigger: 'blur' }
          ],
          docPractice: [
            { required: true, message: '医师执业证书不能为空', trigger: 'blur' }
          ],
          askCount: [
            { required: true, message: '问诊次数不能为空', trigger: 'blur' }
          ],
          docMoney: [
            { required: true, message: '总收入金额不能为空', trigger: 'blur' }
          ],
          docGood: [
            { required: true, message: '好评度不能为空', trigger: 'blur' }
          ],
          docMake: [
            { required: true, message: '预约人次（不超过5人）不能为空', trigger: 'blur' }
          ],
          docImg: [
            { required: true, message: '医生头像不能为空', trigger: 'blur' }
          ],
          docCollect: [
            { required: true, message: '关注人数不能为空', trigger: 'blur' }
          ],
          docInd: [
            { required: true, message: '主治疾病不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.docId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.docId) {
            this.$http({
              url: this.$http.adornUrl(`/aftersystem/doctor/info/${this.dataForm.docId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.docName = data.doctor.docName
                this.dataForm.docType = data.doctor.docType
                this.dataForm.hosId = data.doctor.hosId
                this.dataForm.departId = data.doctor.departId
                this.dataForm.typeId = data.doctor.typeId
                this.dataForm.docYear = data.doctor.docYear
                this.dataForm.docStatus = data.doctor.docStatus
                this.dataForm.docPractice = data.doctor.docPractice
                this.dataForm.askCount = data.doctor.askCount
                this.dataForm.docMoney = data.doctor.docMoney
                this.dataForm.docGood = data.doctor.docGood
                this.dataForm.docMake = data.doctor.docMake
                this.dataForm.docImg = data.doctor.docImg
                this.dataForm.docCollect = data.doctor.docCollect
                this.dataForm.docInd = data.doctor.docInd
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/aftersystem/doctor/${!this.dataForm.docId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'docId': this.dataForm.docId || undefined,
                'docName': this.dataForm.docName,
                'docType': this.dataForm.docType,
                'hosId': this.dataForm.hosId,
                'departId': this.dataForm.departId,
                'typeId': this.dataForm.typeId,
                'docYear': this.dataForm.docYear,
                'docStatus': this.dataForm.docStatus,
                'docPractice': this.dataForm.docPractice,
                'askCount': this.dataForm.askCount,
                'docMoney': this.dataForm.docMoney,
                'docGood': this.dataForm.docGood,
                'docMake': this.dataForm.docMake,
                'docImg': this.dataForm.docImg,
                'docCollect': this.dataForm.docCollect,
                'docInd': this.dataForm.docInd
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
